package com.jaya.budan.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.jaya.budan.R;
import com.jaya.budan.adapter.Square_ViewPagerAdapter;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.bean.ListUser_Information;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.VolleyUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class Square_Detail_Photo_Activity extends BaseActivity implements View.OnClickListener {

    private String access_token;
    private List<ListUser_Information> user_informations;
    private List<ListUser_Information.Files> user_information;
    private int roomId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pop_square_detail);
        Intent intent = getIntent();
        access_token = intent.getStringExtra("access_token");
        roomId = intent.getIntExtra("roomId", 0);
        user_information = (List<ListUser_Information.Files>) intent.getSerializableExtra("information");
        user_informations = (List<ListUser_Information>) intent.getSerializableExtra("user_information");
        ViewPager pop_vp_square_detail = findViewById(R.id.pop_vp_square_detail);
        TextView tv_pop_square_detail = findViewById(R.id.tv_pop_square_detail);
        ImageView iv_pop_square_detail = findViewById(R.id.iv_pop_square_detail);
        if (user_informations != null){
            pop_vp_square_detail.setAdapter(new Square_ViewPagerAdapter(this, user_informations.get(roomId).getFiles()));
        } else {
            pop_vp_square_detail.setAdapter(new Square_ViewPagerAdapter(this, user_information));
        }
        pop_vp_square_detail.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                int count = position + 1;
                if (user_informations != null){
                    tv_pop_square_detail.setText(count + "/" + user_informations.get(roomId).getFiles().size());
                } else {
                    tv_pop_square_detail.setText(count + "/" + user_information.size());
                }
            }

            @Override
            public void onPageSelected(int position) {
                Log.d("square_square", position + "");
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                Log.d("square_square", state + "");
            }
        });
        iv_pop_square_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 网络请求
     */
    private void postData(JSONObject object) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, RequestConnect.gr_xyf_Url + access_token + "&limit=10", object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("xinyun", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONArray info = jsonObject.getJSONArray("info");
                    } else {
                        ToolUtils.showToast(Square_Detail_Photo_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(Square_Detail_Photo_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }
}
